<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */ 
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller {
    public $session_id;
    public $cateTrre = array();
    /*
     * 初始化操作
     */
    public function _initialize() {
//        if(true == isMobile()){
//            header("Location: ".U('Mobile/Index/index'));
//        }
        $action_name=strtolower(ACTION_NAME);
        $controller_name=strtolower(CONTROLLER_NAME);
        $this->assign('action_name',$action_name);
        $this->assign('controller_name',$controller_name);
        if($action_name=='index'&&$controller_name=='index'){
            $this->assign('is_index',1);
        }

        //footer 客服QQ
        $shop_info_config = tpCache('shop_info');
        $this->assign('shop_info_config',$shop_info_config);
        if($shop_info_config['qq'])$qq_list[]=$shop_info_config['qq'];
        if($shop_info_config['qq2']) $qq_list[]=$shop_info_config['qq2'];
        if($shop_info_config['qq3'])$qq_list[]=$shop_info_config['qq3'];
        if($shop_info_config['qq4'])$qq_list[]=$shop_info_config['qq4'];
        if($shop_info_config['qq5']) $qq_list[]=$shop_info_config['qq5'];
        if($shop_info_config['qq6'])$qq_list[]=$shop_info_config['qq6'];
        if($shop_info_config['qq7']) $qq_list[]=$shop_info_config['qq7'];
        if($shop_info_config['qq8']) $qq_list[]=$shop_info_config['qq8'];
        if($shop_info_config['qq9']) $qq_list[]=$shop_info_config['qq9'];
        if($shop_info_config['qq10']) $qq_list[]=$shop_info_config['qq10'];

       $n_qq_list= array_rand($qq_list,4);
       foreach($n_qq_list AS $v){
           $n_qqx_l[]=$qq_list[$v];
       }
        $this->assign('qq_lists',$n_qqx_l);
     /*  dump($n_qq_list);
       dump($n_qqx_l);exit;*/
		//取4个客服QQ
       /* $sales_where['status']=1;
        $rasales_list = M('sales')->where($sales_where)->order('rand()')->limit(4)->select();
        dump($rasales_list);exit;*/
//        $this->assign('rasales_list',$rasales_list);
        //footer 侧边栏 最新公告

        $where_a['is_open']=1;
        $where_a['cat_id']=5;
        $article_list =M('Article')->where($where_a)->order('add_time DESC ,article_id DESC')->limit('6')->select();
        foreach($article_list AS $key=>$value)
        {
            $value["cuttitle"] = getSubstr($value["title"],0,10);
            $article_list[$key] = $value;
        }
        $this->assign('article_list',$article_list);


        //footer 侧边栏 浏览记录
        $look_arr=cookie('look_arr');
        if($look_arr){
            $look_str='';
            foreach($look_arr AS $vv){
                $look_str?$look_str.=','.$vv:$look_str.=$vv;
            }
            $look_where['goods_id']=array('in',$look_str);
            $look_where['status']=1;
            $look_goods =M('goods')->where($look_where)->limit('5')->select();
            $this->assign('look_goods',$look_goods);
        }



        //友情链接

       $friend_link = M('friend_link')->where("is_show=1")->order('orderby ASC')->limit('15')->select();
       $this->assign('friend_link',$friend_link);


		if(!$_COOKIE["hidetopstatus"])
		{
			cookie('hidetopstatus','hide',3600);
		}
		
		if(!$_COOKIE["filterstatus"])
		{
			cookie('filterstatus','show',3600);
		}
		
		if($_COOKIE["think_language"]=="zh-hk")
		{
			$bi="_ft";
		}
		if($_COOKIE["think_language"]=="zh-jp")
		{
			$bi="_jp";
		}
        $this->assign('user_id',$_SESSION['user']['user_id']);
		$this->assign('bi',$bi);
		$this->assign('name','name'.$bi);
		$this->assign('title','title'.$bi);

		//$this->assign('mobile_name','mobile_name'.$bi);
		
    	$this->session_id = session_id(); // 当前的 session_id
        define('SESSION_ID',$this->session_id); //将当前的session_id保存为常量，供其它方法调用
        // 判断当前用户是否手机                
        if(isMobile())
            cookie('is_mobile','1',3600); 
        else 
            cookie('is_mobile','0',3600);
                  
        $this->public_assign();


    }
    /**
     * 保存公告变量到 smarty中 比如 导航 
     */
    public function public_assign()
    {
        
       $tpshop_config = array();
       $tp_config = M('config')->cache(true,TPSHOP_CACHE_TIME)->select();       
       foreach($tp_config as $k => $v)
       {
       	  if($v['name'] == 'hot_keywords'){
       	  	 $tpshop_config['hot_keywords'] = explode('|', $v['value']);
       	  }       	  
          $tpshop_config[$v['inc_type'].'_'.$v['name']] = $v['value'];
       }                        
       
       $goods_category_tree = get_goods_category_tree();    
       $this->cateTrre = $goods_category_tree;
       $this->assign('goods_category_tree', $goods_category_tree);                     
       $brand_list = M('brand')->cache(true,TPSHOP_CACHE_TIME)->field('id,parent_cat_id,logo,is_hot')->where("parent_cat_id>0")->select();              
       $this->assign('brand_list', $brand_list);
       $this->assign('tpshop_config', $tpshop_config);
    }


}